const path = require('path')
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const session = require('express-session')
const compression = require('compression')
const template = require('art-template')
const dateFormat = require('dateformat')
// 压缩
app.use(compression())
// 返回格式
// app.use(require('./middleware/resextra'))

    // 静态文件访问
    // app.use('/static', express.static(path.join(__dirname, 'public')))
app.use('/static', express.static(path.join(__dirname, 'public')))

app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

app.use(session({
    secret: 'keyboard cat',
    resave: false,
    saveUninitialized: false,
    cookie: { maxAge: 60 * 60 * 1000 }
  }))

require('./model/connect')

// 模板引擎设置
app.engine('art', require('express-art-template'));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'art');

template.defaults.imports.dateFormat = dateFormat ;
// 时间戳
// console.log(new Date().getTime());
// console.log(Date.now())
// console.log(+ new Date())
// 路由守卫
app.use('/admin',require('./middleware/loginGuide'))
app.get('/',(req,res)=>{
    res.redirect('/home')
})
app.use('/home', require('./route/home'))
app.use('/admin', require('./route/admin'))


app.use((err,req,res,next)=>{
    console.log(err)
})

app.listen(3000, () => {
    console.log('服务启动，访问http://localhost:3000')
})